package com.numberone.master.modules.profit.web;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.numberone.master.common.persistence.Page;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.modules.profit.dao.AccountInfoDao;
import com.numberone.master.modules.profit.entity.Account;
import com.numberone.master.modules.profit.service.AccountService;
import com.numberone.master.modules.sys.entity.Office;
import com.numberone.master.modules.sys.utils.UserUtils;

/**
 * 利润Controller
 * @author chengkang
 * @version 2018-02-12
 */
@Controller
@RequestMapping(value = "${adminPath}/profit/account")
public class AccountController extends BaseController {

	@Autowired
	private AccountService accountService;
	@Autowired
	private AccountInfoDao accountInfoDao;

	 /**
     * 分润list列表
     * @param account
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequiresPermissions("profit:account:profitList")
	@RequestMapping(value = {"profit_list", ""})
	public String list(Account account, HttpServletRequest request, HttpServletResponse response, Model model) {
		String officeId = UserUtils.getUser().getOffice().getId();
		Double totalMoney = accountService.getTotalMoney(officeId);
		BigDecimal b = new BigDecimal(totalMoney);
		model.addAttribute("totalMoney", b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
		Double profitMoney = accountService.getProfitMoney(officeId);
		BigDecimal p = new BigDecimal(profitMoney);
		model.addAttribute("profitMoney", p.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
		//查询自己的代理商id
		Office office = UserUtils.getUser().getOffice();
		account.setAccountNo(office.getId());
		Page<Account> page = accountService.findPage(new Page<Account>(request, response), account); 
		model.addAttribute("page", page);
		return "modules/profit/profitList";
	}

	/**
	 * 提现列表
	 * @param account
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("profit:account:withdrawaList")
	@RequestMapping(value = {"withdrawa_list", ""})
	public String withdrawaList(Account account, HttpServletRequest request, HttpServletResponse response, Model model) {
		//查询自己的代理商id
		Office office = UserUtils.getUser().getOffice();
    	//已提现和到账金额
    	Double taskMoney = accountInfoDao.getTaskMoney(office.getId());
    	BigDecimal b = new BigDecimal(taskMoney);
    	model.addAttribute("taskMoney", b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
    	Double arrivalMoney = accountInfoDao.getArrivalMoney(office.getId());
    	BigDecimal a = new BigDecimal(arrivalMoney);
    	model.addAttribute("arrivalMoney", a.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
		account.setAccountNo(office.getId());
		Page<Account> page = accountService.withdrawaPage(new Page<Account>(request, response), account); 
		model.addAttribute("page", page);
		return "modules/profit/withdrawaList";
	}

}